package com.cysion.study.ktbox.base

import android.app.Activity
import android.app.Application
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.app.AppCompatDelegate
import android.view.View
import android.widget.TextView
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.WaitDialog
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


abstract class BaseActivity : AppCompatActivity() {

    //避开与某些闭包内的this冲突
    protected val self: Activity by lazy {
        this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT >= 21) {
            val decorView: View = window.decorView
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
            window.statusBarColor = Color.TRANSPARENT
        }
/*注解：getWindow().getDecorView()拿到当前活动DecorView，
再setSystemUiVisibility()改变系统UI的显示
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN和View.SYSTEM_UI_FLAG_LAYOUT_STABLE
就表示活动的布局会显示在状态栏上面，再将状态栏设置为透明色setStatusBarColor()*/
        setContentView(getLayoutId());
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

        EventBus.getDefault().register(this)
        initView()
        initData()

    }
    fun TextView.checkBlank(message: String): String? {
        val text = this.text.toString().trim()
        if (text.isBlank()) {
            ShowToast(message)
            return null
        }
        return text
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        closeMvp()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun fromEventbus(app: Application) {
        //仅占位，防止子类继承出问题
        //子类可以仿照这种写法，但是事件类型必须是自定义的
    }

    abstract fun getLayoutId(): Int

    abstract fun initView()

    protected open fun initData() {
    }

    //若使用了mvp，可在此方法内 解绑view和presenter
    abstract fun closeMvp()

    protected var mWaitDialog: WaitDialog? = null
    open fun showWaitDialog(isNotBackFinish: Boolean) {
        if (self == null) {
            return
        }
        showWaitDialog("", isNotBackFinish)
    }
    open fun hideWaitDialog() {
        if (mWaitDialog != null) {
            mWaitDialog!!.dismiss()
        }
    }

    open fun showWaitDialog(msg: String?, isNotBackFinish: Boolean) {
        if (mWaitDialog != null && mWaitDialog!!.isShowing) {
            return
        }
        if (mWaitDialog == null) {
            mWaitDialog = WaitDialog(self)
        }
        mWaitDialog!!.setCanceledOnTouchOutside(isNotBackFinish)
        mWaitDialog!!.setOnCancelListener {
            if (!isNotBackFinish) {
                mWaitDialog!!.dismiss()
            }
        }
        if (self != null) {
            mWaitDialog!!.showMessage(msg)
        }
    }
}